//
//  BidViewController.m
//  LineMoney
//
//  Created by 范琦 on 16/7/8.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "BidViewController.h"
#import "BidTopView.h"
#import "ExchangeRateView.h"
#import "CompleteTableViewCell.h"
#import "BidDetailViewController.h"
@interface BidViewController ()<UITableViewDataSource, UITableViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource>


@property (nonatomic, strong) BidTopView *topView;
@property (nonatomic, strong) ExchangeRateView *rateView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *bigView;
@property (nonatomic, strong) UIPickerView *pickView;
@property (nonatomic, strong) UIView *pickTopView;
@property (nonatomic, strong) NSMutableArray *yearArr;
@property (nonatomic, strong) NSMutableArray *mounthArr;
@property (nonatomic, strong) NSMutableArray *dayArr;
@property (nonatomic, strong) NSString *str;
@end

@implementation BidViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"投标版";
    [self creatBigView];
    [self creatTopView];
    [self creatRateView];
    [self initTableView];
    

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = NO;
    
}

- (void)creatBigView
{
    _bigView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT)];
    _bigView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_bigView];
    
}
- (void)creatTopView
{
    self.topView = [[[NSBundle mainBundle] loadNibNamed:@"BidTopView" owner:self options:nil] lastObject];
    _topView.frame = CGRectMake(0, 0, KSCREENWIDTH, 32);
    self.topView.backgroundColor = [UIColor colorWithRed:244 / 255.0 green:244 / 255.0 blue:244 / 255.0 alpha:1];
    [[_topView.firstButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self creatPickerView:_topView.firstButton];
    }];
    [[_topView.secondButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self creatPickerView:_topView.secondButton];
    }];
    [_bigView addSubview:_topView];
}

- (void)creatRateView
{
    self.rateView = [[[NSBundle mainBundle] loadNibNamed:@"ExchangeRateView" owner:nil options:nil] lastObject];
    _rateView.frame = CGRectMake(0, _topView.bounds.size.height, KSCREENWIDTH, 50);
    [_bigView addSubview:_rateView];

    _rateView.leftLabel.textColor = TEXTCOLOR;
    _rateView.numLabel.textColor = BACKGROUNDCOLOR;
    
}

#warning 创建PickerView
- (void)creatPickerView:(UIButton *)btn
{
    if (_pickView == nil) {
        self.yearArr = [NSMutableArray arrayWithObjects:@"USA",@"CNY",@"法国", nil];
        self.mounthArr = [NSMutableArray arrayWithObjects:@"7",@"8",@"9",@"10", nil];
        self.dayArr = [NSMutableArray arrayWithObjects:@"15",@"16",@"17",@"18", nil];
        
        self.pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, KSCREENHEIGHT - 150, KSCREENWIDTH, 150)];
        _pickView.frame = CGRectMake(0, KSCREENHEIGHT - 144, KSCREENWIDTH, 144);
        _pickView.delegate = self;
        _pickView.dataSource = self;
        [self.view addSubview:_pickView];
        _pickView.backgroundColor = [UIColor huiseColor];
        
        self.pickTopView = [[UIView alloc]initWithFrame:CGRectMake(0, _pickView.frame.origin.y - 30, KSCREENWIDTH, 30)];
        [self.view addSubview:_pickTopView];
        _pickTopView.backgroundColor = HUIBACKGROUNDCOLOR;
        
    }else{
        [self.view addSubview:_pickView];
        [self.view addSubview:_pickTopView];
    }
    UIButton *done = [UIButton buttonWithType:UIButtonTypeSystem];
    [done setTitle:@"Done" forState:UIControlStateNormal];
    done.frame = CGRectMake(KSCREENWIDTH - 60, 0, 40, 30);
    done.tintColor = [UIColor blackColor];
    [_pickTopView addSubview:done];
    [[done rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [_pickView removeFromSuperview];
        [_pickTopView removeFromSuperview];
        if (btn == _topView.firstButton) {
            
            _topView.fistLabel.text =_str;
        } else {
            _topView.secondLabel.text = _str;
        }

    }];
    
}
#warning PickerView代理方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    return 3;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    //    if (component == 0) {
    //        return _yearArr[row];
    //    }else if (component == 1){
    //        return _mounthArr[row];
    //    }else{
    //
    //        return _dayArr[row];
    //    }
    return _yearArr[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _str = _yearArr[row];
}


- (void)initTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, _topView.bounds.size.height + _rateView.bounds.size.height, KSCREENWIDTH, KSCREENHEIGHT - _topView.bounds.size.height - _rateView.bounds.size.height) style:UITableViewStylePlain];
    [_bigView addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];// 如果没有数据下方不显示分割线
    [_tableView registerNib:[UINib nibWithNibName:@"CompleteTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CompleteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];// 去掉点击之后的阴影
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BidDetailViewController *bidDetail = [[BidDetailViewController alloc] init];
    [self.navigationController pushViewController:bidDetail animated:YES];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 126;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
